package com.example.keli.ui.my.invite;

import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.ui.my.adapter.InviteUserAdapter;
import com.example.keli.ui.my.model.InviteUserVO;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

public class InviteActivity extends BaseActivity {


    @BindView(R.id.recycler_invite)
    RecyclerView recyclerInvite;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;

    private List<InviteUserVO> mListData;
    private InviteUserAdapter inviteUserAdapter;
    private int page = 1;

    @Override
    protected int layoutId() {
        return R.layout.activity_invite;
    }

    @Override
    protected void bindData() {
//        setTitle("邀请详情");
        abTitle.setText("邀请详情");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListData = new ArrayList<>();
        inviteUserAdapter = new InviteUserAdapter(mListData, this);
        recyclerInvite.setLayoutManager(new LinearLayoutManager(this));
        recyclerInvite.setAdapter(inviteUserAdapter);
        recyclerInvite.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(ContextCompat.getColor(this, R.color.divider_line))
                .size(DensityUtil.dip2px(this, 1))
                .build());
        View emptyView = View.inflate(this, R.layout.empty_view, null);
        TextView text = emptyView.findViewById(R.id.tv_empty);
        text.setText("暂无记录");
        inviteUserAdapter.setEmptyView(emptyView);
        loadDatas();
    }

    @Override
    protected void bindListener() {
        super.bindListener();
        refreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mListData.clear();
                loadDatas();
            }
        });
//        inviteUserAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                page++;
//                loadDatas();
//            }
//        }, recyclerInvite);
    }

    private void loadDatas() {
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getMyConvey(), new SubscriberListener<BaseResponse<List<InviteUserVO>>>() {
            @Override
            public void onSuccess(BaseResponse<List<InviteUserVO>> listBaseResponse) {
                List<InviteUserVO> data = listBaseResponse.getData();
                if (page == 1) {
                    refreshlayout.setRefreshing(false);
                }
                if (data == null || data.size() == 0) {
                    inviteUserAdapter.loadMoreEnd();
                    return;
                }
                mListData.addAll(data);
                inviteUserAdapter.notifyDataSetChanged();
                inviteUserAdapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {
                refreshlayout.setRefreshing(false);
            }

            @Override
            public void onTokenError() {
                refreshlayout.setRefreshing(false);
            }
        });
    }

}
